<?php
/**
 * Created by PhpStorm.
 * User: 554665488
 * Date: 2018-6-8
 * Time: 1:22
 * @description:
 * @Author: yfl
 * @QQ 554665488
 */

/**
 * php clone
 */
class Account
{
    public $balance;

    public function __construct($balance)
    {
        $this->balance = $balance;
    }


}


class Person
{

    public $name;

    public $account;

    public function __construct($name, $account)
    {

        $this->name = $name;
        $this->account = $account;
    }

    public function __clone()
    {                                                 #复制方法,可在里面定义再clone是进行的操作
        return $this->account= clone $this->account;  #不加这一句,account在clone是会只被复制引用,其中一个account的balance被修改另一个也同样会被修改
    }


}

$person = new Person('yfl', new Account(1000));

$clonePerson = clone $person;
//$clonePerson->account->balance = 9999;
//echo $clonePerson->account->balance.PHP_EOL;
//echo $person->account->balance;
var_dump($person->name='asdasd');//对象的复制是通过关键字 clone 来实现的。用 clone 克隆出来的对象与原对象没有任何关系，
//它是把原来的对象从当前的位置重新复制了一份，也就是相当于在内存中新开辟了一块空间。通过关键字 clone 可以克隆一个对象
var_dump($clonePerson->name);